package org.mzx.shortLink.adminn.controller;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;

import org.mzx.shortLink.starter.convention.Result.Result;
import org.mzx.shortLink.starter.web.Results;
import org.mzx.shortLink.adminn.dto.Request.LoginDtoReq;
import org.mzx.shortLink.adminn.dto.Request.TUserDtoReq;
import org.mzx.shortLink.adminn.dto.Request.TUserUpdateReq;
import org.mzx.shortLink.adminn.dto.Response.LoginDtoResp;
import org.mzx.shortLink.adminn.dto.Response.UserActualRespDTO;
import org.mzx.shortLink.adminn.dto.Response.UserDto;
import org.mzx.shortLink.adminn.service.TUserService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class TUserController {

    private final TUserService service;

    /**
     * 根据用户id查询用户
     * */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserDto> getByName(@PathVariable("username") String userName) {

        return Results.success(service.getByName(userName));
    }

    /**
     * 判断某个用户id是否可用
     * 注册用
     * */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> isExist(@RequestParam("username") String userName) {
        return Results.success(service.isExist(userName));
    }

    /**
     * 注册新用户
     * */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Boolean> zhuCe(@RequestBody TUserDtoReq user) {
        return Results.success(service.zhuCe(user));
    }

    /**
     * 根据用户名查询无脱敏用户信息
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(service.getByName(username), UserActualRespDTO.class));
    }
    /**
     * 更新
     * @param user
     * @return
     */
    @PostMapping("/api/short-link/admin/v1/user/update")
    public Result<Void> update(@RequestBody TUserUpdateReq user) {
        return Results.success(service.Update(user));
    }

    /**
     * 用户登录
     */
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<LoginDtoResp> login(@RequestBody LoginDtoReq requestParam) {
        return Results.success(service.login(requestParam));
    }

    /**
     * 用户登出
     * */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> loginOut(@RequestParam("username") String username) {
        return Results.success(service.loginOut(username));
    }
}
